package com.zhipu.balance.ui.vm.home.calculate.dami

import android.text.TextUtils
import androidx.databinding.ObservableField
import com.zhipu.balance.mobile.event.Personnel
import com.zhipu.balance.mobile.event.RecordDaMi
import com.zhipu.balance.remotedata.systemapi.SystemApiService
import com.zhipu.balance.ui.vm.BaseViewModel
import com.zhipu.balance.utils.CommonUtils

/**
 * 大米互混
 */
class DaMiIntermixingModel(mSystemApiService : SystemApiService) : BaseViewModel(mSystemApiService){


    /**
     * 重量
     */
    var weighing : String? = null

    /**
     * 大米实体
     */
    var mRecordDaMi : RecordDaMi? = null


    /**
     * 当前的检查人员
     */
    var mPersonnel : Personnel? = null


    /**
     * 互混-试样质量
     */
    var massOfMixedSample = ObservableField("")


    /**
     * 异种粮质量
     */
    var mixedGrainQuality =  ObservableField("")

    /**
     *  互混-结果
     */
    var mixedGrainResult =  ObservableField("")




    /**
     * 更新页面
     */
    fun updateRecordRice(){
        if(!TextUtils.isEmpty(mRecordDaMi?.mixedGrainRecorder)) {
            //互混-试样质量
            massOfMixedSample.set(mRecordDaMi?.massOfMixedSample.toString())
            //异种粮质量
            mixedGrainQuality.set(mRecordDaMi?.mixedGrainQuality.toString())
            //互混-结果
            mixedGrainResult.set(mRecordDaMi?.mixedGrainResult.toString())
        }
    }


    /**
     * 释放更新页面
     */
    fun releaseRecordRice(){
        //互混-试样质量
        massOfMixedSample.set("")
        //异种粮质量
        mixedGrainQuality.set("")
        //互混-结果
        mixedGrainResult.set("")
    }




    /**
     * 试样质量
     */
    fun BulkQuality(){
        massOfMixedSample.set(weighing)
        mRecordDaMi?.massOfMixedSample = massOfMixedSample.get()?.toDouble()
    }

    /**
     * 异种粮质量
     */
    fun QualityOfDifferentCrops(){
        mixedGrainQuality.set(weighing)
        mRecordDaMi?.mixedGrainQuality = mixedGrainQuality.get()?.toDouble()
    }


    /**
     * 计算
     */
    fun calculation(){
        /**
         * 计算互混-结果
         */
        var mMixedGrainResult: Double = CommonUtils.formatDouble(mRecordDaMi!!.mixedGrainQuality / mRecordDaMi!!.massOfMixedSample * 100)
        mRecordDaMi?.mixedGrainResult = mMixedGrainResult
        mixedGrainResult.set(mMixedGrainResult.toString())


        /**
         * 互混记录员
         */
        mRecordDaMi!!.mixedGrainRecorder = mPersonnel!!.name


        //互混-试样质量
        if(mRecordDaMi?.massOfMixedSample == 0.00){
            massOfMixedSample.set("0.00")
        }
        //异种粮质量
        if(mRecordDaMi?.mixedGrainQuality == 0.00){
            mixedGrainQuality.set("0.00")
        }
    }
}